#include "levelsence.h"
#include "gameconfig.h"
#include "gamecontroller.h"
#include "gamepushbutton.h"
#include <QIcon>
#include <QPixmap>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPainter>
#include <QLabel>
LevelSence::LevelSence(QWidget *parent) : QMainWindow(parent)
{
    this->setWindowTitle(tr("关卡选择"));
    this->setFixedSize(GAME_WIDTH, GAME_HEIGHT);
    this->setWindowIcon(QPixmap(IMG_GAME_ICON));

    // menu
    QMenuBar *lvMenuBar = this->menuBar();
    QMenu *menuFile = lvMenuBar->addMenu(tr("文件"));
    QAction *acBack = menuFile->addAction(tr("返回"));
    connect(acBack, &QAction::triggered, this, [=]()
    {
        // 返回游戏界面
        GameController::getInstance()->hideLevelScene();
        GameController::getInstance()->showMainScene();
    });
    QAction *acQuit = menuFile->addAction(tr("退出"));
    connect(acQuit, &QAction::triggered, this, [=]()
    {

        exit(0);

    });

    // 关卡信息
    for (int i = 0; i < LEVEL_COUNT; ++i)
    {
        GamePushButton *btnLevel = new GamePushButton(IMG_GAME_LEVEL_ICON, "");
        btnLevel->setParent(this);
        btnLevel->move(30 + (i % 4) * 90, 140 + (i / 4) * 70);
        //
        int curlevel = i + 1;
//        QLabel *lblLevel = new QLabel(this);
//        lblLevel->setGeometry(btnLevel->x(), btnLevel->y(), btnLevel->width(), btnLevel->height());
//        lblLevel->setAttribute(Qt::WA_TransparentForMouseEvents, true);
        QLabel *lblLevel = new QLabel(btnLevel);
        lblLevel->setGeometry(0, 0, btnLevel->width(), btnLevel->height());
        lblLevel->setAlignment(Qt::AlignCenter);
        lblLevel->setText(QString::number(curlevel));
        //
        connect(btnLevel, &QPushButton::clicked, this, [=]()
        {
           //
           GameController::getInstance()->playFlipCoinSound();
           GameController::getInstance()->hideLevelScene();
           GameController::getInstance()->showGameScene(curlevel);
        });
    }

    // 返回按钮
    GamePushButton *btnBack = new GamePushButton(BTN_BACK_NORMAL, BTN_BACK_PRESSED);
    btnBack->setParent(this);
    btnBack->move(GAME_WIDTH - btnBack->width(), GAME_HEIGHT - btnBack->height());
    connect(btnBack, &QPushButton::clicked, this, [=]()
    {
       // 返回游戏界面
        GameController::getInstance()->playBackSound();
        GameController::getInstance()->hideLevelScene();
        GameController::getInstance()->showMainScene();
    });
}

void LevelSence::paintEvent(QPaintEvent *event)
{
    QPainter levelPainter(this);
    QPixmap levelPix;
    bool ret = levelPix.load(IMG_BG_LEVELSCENE);
    if(ret)
    {
        levelPainter.drawPixmap(0, 0, levelPix);
    }
    //
    bool retTitle = levelPix.load(IMG_GAME_TITLE);
    if(retTitle)
    {
        levelPainter.drawPixmap((GAME_WIDTH - levelPix.width()) / 2, 0, levelPix);
    }
}
